package com.dream.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.ResourcesMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.Imgjson;
import com.dream.pojo.ResourceSon;
import com.dream.pojo.Resources;
import com.dream.pojo.ResourcesExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Classification_able;
import com.dream.service.able.Upload_able;
import com.dream.service.abs.AbsUpload;

/*
 * 资源的业务层
 */
@Service("Resources_ServiceImpl")
public class Resources_ServiceImpl 
extends AbsUpload 
implements Upload_able<Imgjson>,
All_Service_able<ResourceSon>,
Classification_able<ResourceSon> {
	
	/*
	 * 资源表DAO代理对象
	 */
	@Resource(name="resourcesMapper")
	private ResourcesMapper resources_mapper;
	
	/*
	 * (添加资源，依次需要插入，图片地址，标题，简介，用户id，演示地址（可选），下载地址，资源标签id)
	 * @see com.dream.service.able.All_Serviceable#insert(java.lang.String[])
	 */
	@Override
	public ResourceSon insert(String ... str) {
		//切换数据源，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建资源对像
		ResourceSon resources = new ResourceSon();
		//插入图片地址
		resources.setImg(str[0]);
		//插入标题
		resources.setTitle(str[1]);
		//插入简介
		resources.setIntroduce(str[2]);
		//插入用户id
		resources.setUid(Integer.parseInt(str[3]));
		//插入演示地址
		resources.setDemonstration(str[4]);
		//插入下载地址
		resources.setDownload(str[5]);
		//插入资源标签id
		resources.setLid(Integer.parseInt(str[6]));
		//插入状态
		resources.setState(1);
		//写入数据库
		return resources_mapper.insertSelective(resources)>0?resources:null;
	}

	/*
	 * (查询所有资源)
	 * @see com.dream.service.able.All_Serviceable#list()
	 */
	@Override
	public List<ResourceSon> list() {
		//切换数据源，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		ResourcesExample resources_example = new ResourcesExample();
		//创建查询条件工具内部类
		ResourcesExample.Criteria criteria = resources_example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的数据集
		return resources_mapper.listResourceSon(resources_example);
	}

	/*
	 * (查询指定分类的资源)
	 * @see com.dream.service.able.Classification#listClassification(java.lang.Integer)
	 */
	@Override
	public List<ResourceSon> listClassification(Integer lid) {
		//切换数据源。从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		ResourcesExample resources_example = new ResourcesExample();
		//创建查询条件工具内部类
		ResourcesExample.Criteria criteria = resources_example.createCriteria();
		//设置查询条件
		criteria.andLidEqualTo(lid);
		//返回查询到的数据集
		return resources_mapper.listResourceSon(resources_example);
	}

	@Override
	public ResourceSon getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
